package com.wc.AlgoOJ_train.train2_DP训练.c5_区间DP.DP0202_石子合并2;

import java.io.*;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/1/13 12:06
 * @description 石子合并2
 * http://43.138.190.70:8888/p/DP0202
 */

/**
 * 整体思路：将链破成一维的
 * 就完成了
 * 1 2 3 4 5 6 1 2 3 4 5
 * 1 ~ n 2 ~ n -1 3 ~ n - 2
 */
public class Main {
    static int maxN = 500 + 7;
    // dp[i][j] 表示从i到j的最大值
    static int[][] dp = new int[maxN][maxN];
    static int[] stones = new int[maxN];
    static int[] sum = new int[maxN];
    static int n;
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int INF = 0x3f3f3f;

    public static void main(String[] args) {
        n = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            stones[i] = sc.nextInt();
            sum[i] = sum[i - 1] + stones[i];
        }
        for (int i = 1; i <= n; i++) {
            sum[i + n] = sum[i + n - 1] + stones[i];
        }

        for (int i = 2; i <= n; i++) {
            // 起点
            for (int j = 1; j + i - 1 <= 2 * n - 1; j++) {
                // 终点
                int end = j + i - 1;
                int val = sum[end] - sum[j - 1];
                // 设置最大值
                dp[j][end] = INF;
                // 枚举分割点
                for (int k = j; k < end; k++) {
                    dp[j][end] = Math.min(dp[j][end], dp[j][k] + dp[k + 1][end] + val);
                }
            }
        }
        int ans = INF;
        for (int i = 1; i < n; i++) {
            ans = Math.min(ans, dp[i][i + n - 1]);
        }
        out.println(ans);
        out.flush();
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }
}
